package com.orm.handler;

import java.sql.*;
import java.time.LocalDateTime;


/**
 * LocalDateTime 实际是用 Timestamp 来做了一次转换的
 */
public class LocalDateTimeTypeHandler extends BaseTypeHandler<LocalDateTime>{
	
	@Override
	public void setNotNullParameter(PreparedStatement ps, int i, LocalDateTime parameter, JDBCType jdbcType) throws SQLException {
		ps.setTimestamp(i, Timestamp.valueOf(parameter));
	}
	
	@Override
	public LocalDateTime getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return getLocalDateTime(rs.getTimestamp(columnName));
	}
	
	@Override
	public LocalDateTime getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return getLocalDateTime(rs.getTimestamp(columnIndex));
	}
	
	public LocalDateTime getLocalDateTime(Timestamp timestamp){
		if(timestamp != null){
			return timestamp.toLocalDateTime();
		}
		return null;
	}
}
